<?php

namespace Home\Controller;

/**
 * Description of DealerManageController.class控制器实例
 *
 * @author zhangzhen
 * @date 2017-01-03
 */
class DealerManageController extends DealerParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
    }
    /**
     * @description 客栈开通申请
     * @modify zhangzhen 2017-01-05
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function addHotel() {
        $params = I('post.');
        $validateRule = array(
            'hotelname' => 'notnull,notempty,nameLength20',
            'address' => 'notnull,notempty,nameLength20',
            'mobile' => 'notnull,notempty',
            'username' => 'notnull,notempty,nameLength20',
            'universalTime' => 'notnull,notempty',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
            'currencyId' => 'notnull,notempty,id',
        );

        //检查参数
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $params['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));

        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        $hotelName = $params['hotelname'];
        $cityName = $params['address'];
        $userName = $params['username'];
        $universalTime = isset($params['universalTime']) && !empty($params['universalTime']) ? $params['universalTime'] : '5';
        $cityId = $params['city'];
        $districtId = $params['district'];
        $countryId = isset($params['countryId']) ? $params['countryId'] : null;
        $currencyId = $params['currencyId'];

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
       /*$hotelApplyLogic->apply($hotelName, $cityName, $mobile, $userName, $dealerId, '', $universalTime, $areaCode, $cityId, $districtId);*/
        $hotelApplyLogic->addApplyHotel($hotelName, $cityName, $mobile, $userName, $universalTime, $areaCode, $countryId, $cityId, $districtId, $currencyId);
        returnJson(200, 200);
    }

    public function addUserHotel() {
        $params = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
            'mobile' => 'notnull,notempty',
            'enddate' => 'notnull,notempty,isDate',
            'universalTime' => 'notnull,notempty',
            'currencyId' => 'notnull,notempty,id',
        );

        //检查参数
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $params['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));
        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        $name = $params['hotel'];
        $cityId = $params['city'];
        $districtId = $params['district'];
        $enddate = $params['enddate'];
        $universalTime = $params['universalTime'];
        $currencyId = $params['currencyId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->addUserInfo($mobile, $name, $cityId, $districtId, $enddate, $universalTime, $areaCode, $currencyId);
        returnJson(200, 200, 'success');
    }

     /**
     * @description 经销商添加义卖客栈
     * @modify wenhui 2017-09-18
     */
    public function addUserCharityHotel() {
        $params = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
            'mobile' => 'notnull,notempty',
            'enddate' => 'notnull,notempty,isDate',
            'universalTime' => 'notnull,notempty',
            'currencyId' => 'notnull,notempty,id',
        );

        //检查参数
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $params['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));
        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        $name = $params['hotel'];
        $cityId = $params['city'];
        $districtId = $params['district'];
        $enddate = $params['enddate'];
        $universalTime = $params['universalTime'];
        $currencyId = $params['currencyId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->addUserCharityHotel($mobile, $name, $cityId, $districtId, $enddate, $universalTime, $areaCode, $currencyId);
        returnJson(200, 200, 'success');
    }

    /**
     * @description 客栈开通申请
     * @modify zhangzhen 2017-01-05
     */
    public function saveHotel() {
        $params = I('post.');
        $validateRule = array(
        	'applyId' => 'notnull,notempty,id',
            'hotelname' => 'notnull,notempty,nameLength20',
            'address' => 'notnull,notempty,nameLength20',
            //'mobile' => 'notnull,notempty,mobile',
            'username' => 'notnull,notempty,nameLength20',
        );
        //检查参数
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $params['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));
        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        $applyId = $params['applyId'];
        $hotelName = $params['hotelname'];
        $cityName = $params['address'];
        $userName = $params['username'];
        $dealerId = getDealerId();
        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->updateApply($applyId, $hotelName, $cityName, $mobile, $userName);
        returnJson(200, 200);
    }

    /**
     * @description 修改年限
     * @modify zhangzhen 2017-01-06
     */
    public function setEndtime() {
		$params = I('post.');
		$validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'enddate' => 'notnull,notempty,isDate',
        );   
        //检查参数
        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule); 
        $hotel = $params['hotel'];
        $enddate = $params['enddate'];
        $hotelEntityModel = D('HotelEntity');
        $result = $hotelEntityModel->setDate($hotel, $enddate);
        if ($result) {
        	returnJson(200, 200, $enddate);
        }else{
        	returnJson(500, 508);
        }
    }

    /**
     * @description 启用禁用客栈
     * @modify zhangzhen 2017-01-06
     */
    public function setActive() {
		$params = I('post.');
		$validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'status' => 'notnull,notempty',
        );   
        //检查参数
        $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule); 
        $hotel = $params['hotel'];
        $status = $params['status'];  
        $hotelLogic = A('Common/Hotel', 'Logic');   
        $result = $hotelLogic->dealerSetHotelActive($hotel, $status);
        if ($result) {
        	returnJson(200, 200, $result);
        }else{
        	returnJson(500, 508);
        }
    }

    /**
     * @description 检查用户是否有默认货币
     * @modify wenui 2018-02-10
     */
    public function adminCheckDefaultCurrency() {
        $params = I('post.');
        
        $mobile = $params['mobile'];
        $areaCode = $params['areaCode'];
        //检查账号是否属于该经销商
        $adminLoginModel = D('AdminLogin');
        $adminLogin = $adminLoginModel->getInfoByMobile($mobile, $areaCode);

        $userId = $adminLogin['id'];
        // 获取用户管理客栈id
        $AdminHotelModel = D('AdminHotel');
        $adminHotelInfo = $AdminHotelModel->getInfosByAdmin($userId);

        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrencyAndDefault($adminHotelInfo[0]['hotelentity_id']);
        $currencyData = my_sort($currencyData, 'type', SORT_DESC)[0];
        if ($currencyData['type'] != 1) {
            $currencyData = array();
        }

        returnJson(200, 200, $currencyData);
    }
}
